package com.cn.Stream;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamFiler {
    @Test
    public void test1() {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
        List<Integer> evenNumbers = numbers.stream().filter(n -> n % 2 == 0).collect(Collectors.toList());
        System.out.println(evenNumbers);
    }

    @Test
    public void test2() {
        List<Cat> cats = Arrays.asList(new Cat("Tom", 1), new Cat("Jerry", 2), new Cat("Mickey", 3));
        List<Cat> oldCats = cats.stream().filter(c -> c.getAge() > 2).collect(Collectors.toList());
        System.out.println(oldCats);
    }

    @Test
    public void test3() {
        List<Cat> cats = Arrays.asList(new Cat("Tom", 1), new Cat("Jerry", 2), new Cat("Mickey", 3));
        Cat cat = cats.stream().filter(c -> "Tom".equals(c.getName())).findFirst().orElse(null);
        System.out.println();
    }

}

class Cat {
    private String name;
    private int age;

    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}